{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day06 掌握`()`的用法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　通过前面日程的学习，我们已经可以利用**正则表达式**完成很多常见内容匹配任务，但还不能处理类似这样的情况：\n",
    "\n",
    "> 对于下面的这段字符串，请匹配出所有夹在小写字母之间的连续数字内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-02T06:36:36.215688Z",
     "iopub.status.busy": "2020-09-02T06:36:36.215688Z",
     "iopub.status.idle": "2020-09-02T06:36:36.228652Z",
     "shell.execute_reply": "2020-09-02T06:36:36.227662Z",
     "shell.execute_reply.started": "2020-09-02T06:36:36.215688Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['abcd000ads', 'ada00012dad']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "s = 'abcd000ads  90  ad00  90ad ada00012dad'\n",
    "\n",
    "# 按照之前日程所学知识利用正则找到对应模式\n",
    "re.findall('[a-z]+[0-9]+[a-z]+', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　这样虽然说确实把符合模式设定的字符串匹配到了，但我们需要的是连续数字部分，并不需要两侧的小写字母，这时我们就可以使用`()`来圈定**子表达式**范围，它帮助我们告诉**正则表达式**在整个正则匹配到的片段中，我们真正需要返回的内容在哪些地方："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-02T06:36:36.229653Z",
     "iopub.status.busy": "2020-09-02T06:36:36.229653Z",
     "iopub.status.idle": "2020-09-02T06:36:36.234639Z",
     "shell.execute_reply": "2020-09-02T06:36:36.234639Z",
     "shell.execute_reply.started": "2020-09-02T06:36:36.229653Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['000', '00012']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将中间的连续数字部分用()圈定\n",
    "re.findall('[a-z]+([0-9]+)[a-z]+', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　并且在一个**正则表达式**中我们还可以利用`()`圈定出多个子表达式，进而实现成对、成组匹配结果的返回，每一组匹配内容形成了元组，就像下面的例子一样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-02T06:36:36.236629Z",
     "iopub.status.busy": "2020-09-02T06:36:36.236629Z",
     "iopub.status.idle": "2020-09-02T06:36:36.244608Z",
     "shell.execute_reply": "2020-09-02T06:36:36.243611Z",
     "shell.execute_reply.started": "2020-09-02T06:36:36.236629Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('key1', 'value2'), ('key2', 'value2'), ('key3', 'value3')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 'a = {\"key1\": \"value2\", \"key2\": \"value2\", \"key3\": \"value3\"}'\n",
    "\n",
    "re.findall('\"(.*?)\": \"(.*?)\"', s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-02T06:36:36.245606Z",
     "iopub.status.busy": "2020-09-02T06:36:36.245606Z",
     "iopub.status.idle": "2020-09-02T06:36:36.251591Z",
     "shell.execute_reply": "2020-09-02T06:36:36.250592Z",
     "shell.execute_reply.started": "2020-09-02T06:36:36.245606Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('c', 'c', 'c')]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '[[1, 2, 3], [a, b, 1], [1, a, 1], [c, c, c]]'\n",
    "\n",
    "re.findall('\\[([a-z]+), ([a-z]+), ([a-z]+)\\]', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Day06 课后小测验\n",
    "\n",
    "　　相信通过上面的内容，你已经对`()`的用法有了一定了解，熟能生巧，请你在下面的字符串中匹配出形式如`(时间跨度，节气)`的信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-02T06:36:36.252588Z",
     "iopub.status.busy": "2020-09-02T06:36:36.252588Z",
     "iopub.status.idle": "2020-09-02T06:36:36.257574Z",
     "shell.execute_reply": "2020-09-02T06:36:36.256576Z",
     "shell.execute_reply.started": "2020-09-02T06:36:36.252588Z"
    }
   },
   "outputs": [],
   "source": [
    "target = '2月3-4日对应节气为立春，2月18-19日对应节气为雨水，3月5-6日对应节气为惊蛰，3月20-21日对应节气为春分，12月21-23日对应节气为冬至。'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('2月3-4', '立春'), ('2月18-19', '雨水'), ('3月5-6', '惊蛰'), ('3月20-21', '春分')]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.findall('([0-9]+.+?[0-9-]+).+?为(.+?)，',target)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-26T12:55:19.898241Z",
     "iopub.status.busy": "2020-08-26T12:55:19.897245Z",
     "iopub.status.idle": "2020-08-26T12:55:19.904225Z",
     "shell.execute_reply": "2020-08-26T12:55:19.903227Z",
     "shell.execute_reply.started": "2020-08-26T12:55:19.898241Z"
    }
   },
   "source": [
    "　　请将你的答案截图发到本帖评论区~"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
